package com.slixils.demo.controller;


import com.slixils.demo.pojo.entity.User;
import com.slixils.demo.service.GoodsService;
import com.slixils.demo.vo.GoodsVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author Helen
 * @since 2022-04-05
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private  GoodsService gongService;

    @ApiOperation("商品列表")
    @RequestMapping(value = "/toList", produces = "text/html;charset=utf-8", method = RequestMethod.GET)
    public String toList(Model model, User user) {
        if(user==null){
            return "login";
        }
        model.addAttribute("user",user);
        model.addAttribute("goodsList",gongService.findGoodsVo());
        return "goodsList";
    }
    @RequestMapping("toDetail/{goodsId}")
    public String toDetail(Model model, User user, @PathVariable Long goodsId){
        model.addAttribute("user",user);
        GoodsVo goods = gongService.findGoodsVoByGoodsId(goodsId);
        model.addAttribute("goods",goods);
        Date startDate = goods.getStartDate();
        Date endDate = goods.getEndDate();
        Date newdate = new Date();
        //秒杀状态
        int secKillStatus = 0;
        //剩余开始时间
        int remainSeconds = 0;
        //秒杀还未开始
        if (endDate.before(startDate)) {
            remainSeconds = (int) ((startDate.getTime()-newdate.getTime())/1000);
            // 秒杀已结束
        } else if (newdate.after(endDate)) {
            secKillStatus = 2;
            remainSeconds = -1;
            // 秒杀中
        } else {
            secKillStatus = 1;
            remainSeconds = 0;
        }
        model.addAttribute("secKillStatus",secKillStatus);
        model.addAttribute("remainSeconds",remainSeconds);
        return "goodsDetail";
    }

}

